//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.example.bootintegration.configuration;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Configuration
@AutoConfigureBefore({JacksonAutoConfiguration.class})
public class CanonicalDataBindConfiguration implements ApplicationContextAware {
    private ApplicationContext ctx;

    public CanonicalDataBindConfiguration() {
    }

    @Bean
    public FactoryBean<ObjectMapper> sensitiveJacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        return new SensitiveModifierFactoryBean(builder);
    }

    @Bean
    @Primary
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        return builder.createXmlMapper(false).build();
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return (builder) -> {
            List<Module> modules = new ArrayList();
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            modules.add(javaTimeModule);
            javaTimeModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
            // javaTimeModule.addDeserializer(ZonedDateTime.class, new LenientZonedDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
            // javaTimeModule.addSerializer(LocalDateTime.class, new StripTimezoneLocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
            // javaTimeModule.addDeserializer(LocalDateTime.class, new LenientLocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")));
            // javaTimeModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer(new DateDeserializers.DateDeserializer(), (new StripTimeZoneDateFormat()).withColonInTimeZone(false).withTimeZone(TimeZone.getDefault()), "yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
            // javaTimeModule.addDeserializer(Timestamp.class, new DateDeserializers.TimestampDeserializer(new DateDeserializers.TimestampDeserializer(), (new StripTimeZoneDateFormat()).withColonInTimeZone(false).withTimeZone(TimeZone.getDefault()), "yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
            // builder.serializerByType(Page.class, new LenientPageSerializer());
            // builder.serializerByType(JsonEntity.class, new JsonEntitySerializer());

            builder.featuresToDisable(new Object[]{SerializationFeature.WRITE_DATES_AS_TIMESTAMPS}).featuresToDisable(new Object[]{SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS}).featuresToDisable(new Object[]{DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE}).featuresToDisable(new Object[]{SerializationFeature.FAIL_ON_EMPTY_BEANS}).featuresToDisable(new Object[]{DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES}).modules(modules);
        };
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ctx = applicationContext;
    }
}
